// Name: stats.js
// Description: A script to import data from education.csv and 
// render it as a barchart.
// Written by:  Tanya L. Crenshaw
// Modified by: Ailes, Alraddadi, Jorgensen, Mercer
	
function statsSetup(whichRadioButtonSelected) {
	
	//Define variables to hold Average, Max, and Minimum for all zipcodes
	var avgVal = 0;
	var maxVal = 0;
	var minVal = 0;
	
	//Define averages for GPA, SAT, and student applied
	var avgSat = 0 ;
	var avgGpa = 0;
	var avgApplied = 0;
	var avgEnrolled = 0;
	
	//Define a counter
	var count = 0 ;
	
	//Loop through zipcodes to get averages.
	for (var key in zipCodes){
		avgSat = avgSat + zipCodes[key].getAvgSat();	
		avgGpa = avgGpa + zipCodes[key].getAvgGpa();
		avgEnrolled = avgEnrolled + zipCodes[key].enrolled;
		avgApplied = avgApplied + zipCodes[key].applied;
		count++;
	}
	
	//Get the averages by dividing over the total of zipcodes.
	 avgSat = avgSat / count;
	 avgGpa = avgGpa / count;
	 avgEnrolled = avgEnrolled / count;
	 avgApplied = avgApplied / count;
	
	// clear #stats div
	$( "#stats" ).html("");
 
	//Check if selection coming from index.html is SAT, GPA, student enrolled, or student applied.
	//SAT
	if(whichRadioButtonSelected == 'SAT'){
		avgVal = avgSat;
		maxVal = maxSat;
		minVal = minSat;		
	}
	//GPA
	else if(whichRadioButtonSelected == 'GPA'){
		avgVal = avgGpa;
		maxVal = maxGpa;
		minVal = minGpa;
	}
	//Applicants
	else if(whichRadioButtonSelected == 'Applied'){
		avgVal = Math.round(avgApplied);
		maxVal = Math.round(maxApplied);
		minVal = Math.round(minApplied);
	}
	//Enrolled
	else if(whichRadioButtonSelected == 'Enrolled'){
		avgVal = Math.round(avgEnrolled);
		maxVal = Math.round(maxEnrolled);
		minVal = Math.round(minEnrolled);
	}	 
	
	// From the html body, select the #stats container to be the 
	// parent of two divs with classes bar and label, respectively.
	// Then fill each of those with a div for each bar/label combo.
	// The class attribute allows the .css style information
	// to be applied to the container and its contents.
	
	var label = d3.select("#stats")
		.append("div")
		.attr("class","label")
		
	var bar = d3.select("#stats")
		.append("div")
		.attr("class","bar")
		
	label.append("div")
		.text("Maximum")
		
	bar.append("div")
		.style("width", "100%")
		.text(Math.round(maxVal * 100) / 100)
	
	label.append("div")
		.text("Average")
		
	bar.append("div")
		.style("width",(avgVal/maxVal)*100 + "%")
		.text(Math.round(avgVal * 100) / 100)
	
	if(whichRadioButtonSelected == 'GPA' || whichRadioButtonSelected == 'SAT') {
		label.append("div")
			.text("Minimum")
			
		bar.append("div")
			.style("width",(minVal/maxVal)*100 + "%")
			.text(Math.round(minVal * 100) / 100)
	}
}

